package company.folder.beans;

import company.abstractClasses.AbstractCompanyAdquiredProfileCapableFilterList;
import entities.CompanyFolder;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import comparators.CompanyAdquiredProfileCapableComparator;
import entities.CompanyAdquiredProfileFolder;
import facades.AbstractFacade;
import facades.CompanyAdquiredProfileFolderFacade;
import java.util.Collections;
import javax.enterprise.event.TransactionPhase;
import predicates.CompanyAdquiredProfileCapableSearchCapablePredicate;
import qualifiers.ChosenEntityEvent;
import qualifiers.EditedEntityEvent;
import qualifiers.PreRemoveEntityEvent;
import qualifiers.RemovedEntityEvent;

/**
 *
 * @author damfercer
 */
@Named
@SessionScoped
public class NewViewFolderBean extends AbstractCompanyAdquiredProfileCapableFilterList<CompanyAdquiredProfileFolder, AbstractFacade<CompanyAdquiredProfileFolder>, CompanyAdquiredProfileCapableComparator<CompanyAdquiredProfileFolder,CompanyAdquiredProfileCapableSearchCapablePredicate<CompanyAdquiredProfileFolder>>, CompanyAdquiredProfileCapableSearchCapablePredicate<CompanyAdquiredProfileFolder>> implements Serializable {
    @Inject
    CompanyAdquiredProfileFolderFacade companyAdquiredProfileFolderFacade;
    @Inject
    @EditedEntityEvent
    private Event<CompanyAdquiredProfileFolder>editedCompanyAdquiredProfileFolder;
    private CompanyFolder currentFolder;
    @Inject
    @EditedEntityEvent
    Event<CompanyFolder> editedFolderEvent;
    @Inject
    @PreRemoveEntityEvent
    Event<CompanyAdquiredProfileFolder> preRemoveProfileFolderEvent;
    @Inject
    @RemovedEntityEvent
    Event<CompanyAdquiredProfileFolder> removedProfileFolderEvent;
    
    public void removeCompanyAdquiredProfileFolder(){
        if(getChosenEntity()!=null){
            deleteChosenEntity();
            currentFolder.getCompanyAdquiredProfileFolderList().remove(getChosenEntity());
            //getChosenEntity().getCompanyAdquiredProfile().getCompanyAdquiredProfileFolderList().remove(getChosenEntity());
            //editedFolderEvent.fire(currentFolder);
            this.getEntityList().remove(getChosenEntity());
            this.getBaseEntityList().remove(getChosenEntity());
            //Collections.replaceAll(getCompany().getCompanyAdquiredProfileList(), getChosenEntity().getCompanyAdquiredProfile(), getChosenEntity().getCompanyAdquiredProfile());
            //Collections.replaceAll(getCompany().getCompanyFolderList(), currentFolder, currentFolder);
            this.setChosenEntity(null);
        }
    }
    
    
    public void onSelectedFolder(@Observes(during = TransactionPhase.AFTER_SUCCESS) @ChosenEntityEvent CompanyFolder folder){
        this.reinitFilter();
        this.currentFolder=folder;
        setChosenEntity(null);
        initCandidates();
    }

    public void saveNewAdquiredProfileFolderState(){
        companyAdquiredProfileFolderFacade.edit(getChosenEntity());
        editedCompanyAdquiredProfileFolder.fire(getChosenEntity());
        Collections.replaceAll(getBaseEntityList(), getChosenEntity(), getChosenEntity());
        Collections.replaceAll(getChosenEntity().getCompanyAdquiredProfile().getCompanyAdquiredProfileFolderList(), getChosenEntity(), getChosenEntity());
        Collections.replaceAll(getCompany().getCompanyAdquiredProfileList(), getChosenEntity().getCompanyAdquiredProfile(), getChosenEntity().getCompanyAdquiredProfile());
        Collections.replaceAll(currentFolder.getCompanyAdquiredProfileFolderList(), getChosenEntity(), getChosenEntity());
        Collections.replaceAll(getCompany().getCompanyFolderList(), currentFolder, currentFolder);
        editedFolderEvent.fire(currentFolder);
    }
    
    /**
     * @return the currentFolder
     */
    public CompanyFolder getCurrentFolder() {
        return currentFolder;
    }

    /**
     * @param currentFolder the currentFolder to set
     */
    public void setCurrentFolder(CompanyFolder currentFolder) {
        this.currentFolder = currentFolder;
    }
        
    @Override
    public String redirectToViewEntity(){
        return "/views/company/folder/ViewCandidateView?faces-redirect=true";
    }
    
    

    @Override
    public void initCandidates(){
        setBaseEntityList(new ArrayList<CompanyAdquiredProfileFolder>(currentFolder.getValidCompanyAdquiredProfileFolderList()));
        setEntityList(new ArrayList<CompanyAdquiredProfileFolder>(getBaseEntityList()));
        if(getPredicate()!=null){
            filterFromBaseList();
            sort();
        }
    }

}
